package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.Gwat810Service;
import cn.e4j.modular.api.service.Gwat820Service;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.GwaaT;
import cn.e4j.modular.crm.entity.GwcwT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 订单安装评价 控制类
 * Created by shixc on 2018/5/8.
 */
@RestController
@RequestMapping("api/wx/gwat820")
public class Gwat820Controller extends ControllerSupport {
    @Autowired
    Gwat820Service gwat820Service;

    @Autowired
    Gwat810Service gwat810Service;

    /**
     * 获取业务人员 订单安装评价
     *
     * @return
     */
    @RequestMapping(value = "getGwcvDatas")
    public ResponseData getGwcsDatas(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<Dto>();
        SysUser user = wapper.getSysUser();
        try {
            QueryWrapper<GwaaT> gwaaTQueryWrapper = new QueryWrapper<>();
            gwaaTQueryWrapper.eq("GWAAENT", inDto.getString("ENT"));
            gwaaTQueryWrapper.eq("GWAA001", user.getACCOUNT());
            GwaaT gwaaT = new GwaaT().selectOne(gwaaTQueryWrapper);
            inDto.put("GWAA001", user.getACCOUNT());
            //Dto dto = gwat820Service.getGwaaData(inDto);
            if (gwaaT.getGWAA007().equals("0")) { //gwak002:1:客户2:厂商3:两者
                if (inDto.getString("status").equalsIgnoreCase("N")) {  //status:N未评价
                    list = gwat820Service.getGwcv1(wapper.getPage(),inDto);
                } else if (inDto.getString("status").equalsIgnoreCase("Y")) {
                    list = gwat820Service.getGwcv2(wapper.getPage(),inDto);
                } else {
                    list = gwat820Service.getGwcv3(wapper.getPage(),inDto);
                }
            } else if (gwaaT.getGWAA007().equals("1")) {
                if (inDto.getString("status").equalsIgnoreCase("N")) {
                    list = gwat820Service.getGwcv4(wapper.getPage(),inDto);
                } else if (inDto.getString("status").equalsIgnoreCase("Y")) {
                    list = gwat820Service.getGwcv5(wapper.getPage(),inDto);
                } else {
                    list = gwat820Service.getGwcv6(wapper.getPage(),inDto);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 保存订单评价信息
     *
     * @return
     */
    @RequestMapping(value = "saveData")
    public ResponseData saveData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        GwcwT gwcw_tPO = new GwcwT();
        List<GwcwT> gwcw_tPOs = new ArrayList<>();
        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
            String gwcw004 = inJson.getString("GWCW004");
            String[] gwcw004s = gwcw004.split(",");
            String gwcw005 = inJson.getString("GWCW005");
            String[] gwcw005s = gwcw005.split(",");
            gwcw_tPO = CommonUtils.toJavaClass(inDto,GwcwT.class);
            gwcw_tPO.setGWCWENT(inJson.getString("GWCVENT"));
            gwcw_tPO.setGWCWDOCNO("SPPJ" + DateUtils.getDate("yyyyMMddHHmmssSSS"));
            LocalDate now = LocalDate.now();
            gwcw_tPO.setGWCW001("3");
            gwcw_tPO.setGWCW002(inJson.getString("GWCVDOCNO"));
            gwcw_tPO.setGWCW003(inJson.getBigDecimal("GWCVSEQ"));
            gwcw_tPO.setGWCW006(inJson.getString("GWCW006"));
            gwcw_tPO.setGWCW007(inJson.getString("GWCW007"));
            gwcw_tPO.setGWCW008(inJson.getString("GWCW008"));

            QueryWrapper<GwaaT> gwaaTQueryWrapper = new QueryWrapper<>();
            gwaaTQueryWrapper.eq("GWAAENT", inDto.getString("ENT"));
            gwaaTQueryWrapper.eq("GWAA001", currentUser.getACCOUNT());
            GwaaT gwaaT = new GwaaT().selectOne(gwaaTQueryWrapper);
            
            gwcw_tPO.setGWCW009(gwaaT.getGWAA002());
            gwcw_tPO.setGWCW010(gwaaT.getGWAA004());
            gwcw_tPO.setGWCW011(gwaaT.getGWAA007());
            gwcw_tPO.setGWCW012(gwaaT.getGWAA003());
            gwcw_tPO.setGWCW020(inJson.getString("GWCS001"));
            gwcw_tPO.setGWCWORIU(currentUser.getID());
            for (int i = 0; i < gwcw004s.length; i++) {
                GwcwT gwcw_tPO_ = CommonUtils.toJavaClass(gwcw_tPO,GwcwT.class);
                gwcw_tPO_.setGWCWDOCDATE(now);
                gwcw_tPO_.setGWCWSEQ(BigDecimal.valueOf(i + 1));
                gwcw_tPO_.setGWCW004(gwcw004s[i]);
                gwcw_tPO_.setGWCW005(gwcw005s[i]);
                gwcw_tPOs.add(gwcw_tPO_);
            }
            gwat820Service.saveData(gwcw_tPOs);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 获取业务人员 订单运输评价
     *
     * @return
     */
    @RequestMapping(value = "getMmrj")
    public ResponseData getMmrj(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<Dto>();
        try {
            inDto.put("MMRJENT", inDto.getString("ENT"));
            list = gwat810Service.getMmrj(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

}
